package com.qiguliuxing.dts.job.task;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.google.common.collect.Maps;
import com.qiguliuxing.dts.common.constant.CommConsts;
import com.qiguliuxing.dts.common.constant.CouponConsts;
import com.qiguliuxing.dts.common.constant.CouponUserConsts;
import com.qiguliuxing.dts.db.domain.DtsCoupon;
import com.qiguliuxing.dts.db.domain.DtsCouponUser;
import com.qiguliuxing.dts.service.base.DtsCouponService;
import com.qiguliuxing.dts.service.base.DtsCouponUserService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

import static org.slf4j.LoggerFactory.getLogger;

/**
 * 优惠券过期检测job
 * @author qiguliuxing
 * @since 1.0.0
 */
@Component
public class CouponExpireJob implements SimpleJob {
    private static final Logger logger = getLogger(CouponExpireJob.class);

    @Autowired
    private DtsCouponService couponService;

    @Autowired
    private DtsCouponUserService couponUserService;

    /**
     * 执行作业.
     *
     * @param shardingContext 分片上下文
     */
    @Override
    public void execute(ShardingContext shardingContext) {
        logger.info(String.format("Thread ID: %s, 作业分片总数: %s, " +
                        "当前分片项: %s.当前参数: %s," +
                        "作业名称: %s.作业自定义参数: %s",
                Thread.currentThread().getId(),
                shardingContext.getShardingTotalCount(),
                shardingContext.getShardingItem(),
                shardingContext.getShardingParameter(),
                shardingContext.getJobName(),
                shardingContext.getJobParameter()
        ));

        // 检查优惠券已经失效的记录
        Map<String, Object> couponParamsMap = Maps.newHashMapWithExpectedSize(CommConsts.INIT_HASH_MAP_SIZE);
        couponParamsMap.put("shardingTotalCount", shardingContext.getShardingTotalCount());
        couponParamsMap.put("shardingItem", shardingContext.getShardingItem());

        couponParamsMap.put("status", CouponConsts.STATUS_NORMAL);
        couponParamsMap.put("timeType", CouponConsts.TIME_TYPE_TIME);
        couponParamsMap.put("endTime", LocalDate.now());
        List<DtsCoupon> couponList = couponService.queryExpired(couponParamsMap);
        logger.info("获取到需要设置优惠券失效的总记录数为：{} 条", couponList.size());
        for (DtsCoupon coupon : couponList) {
            coupon.setStatus(CouponConsts.STATUS_EXPIRED);
            couponService.updateById(coupon);
        }

        // 检查用户未使用的优惠券
        Map<String, Object> couponUserParamsMap = Maps.newHashMapWithExpectedSize(CommConsts.INIT_HASH_MAP_SIZE);
        couponUserParamsMap.put("shardingTotalCount", shardingContext.getShardingTotalCount());
        couponUserParamsMap.put("shardingItem", shardingContext.getShardingItem());
        couponUserParamsMap.put("status", CouponUserConsts.STATUS_USABLE);
        couponUserParamsMap.put("endTime", LocalDate.now());

        List<DtsCouponUser> couponUserList = couponUserService.queryExpired(couponUserParamsMap);
        logger.info("获取到用户未使用优惠券已过期的总记录数为：{} 条", couponList.size());
        for (DtsCouponUser couponUser : couponUserList) {
            couponUser.setStatus(CouponUserConsts.STATUS_EXPIRED);
            couponUserService.update(couponUser);
        }
    }
}
